<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" customClass="customWidth" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="预算标题" prop="title">
				<el-input v-model="form.title" placeholder="预算标题" maxlength="80" clearable></el-input>
			</el-form-item>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="预算年度" prop="expenseYear">
						<el-date-picker v-model="form.expenseYear" placeholder="预算年度" type="year" value-format="YYYY" style="width: 100%;"></el-date-picker>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="预算金额" prop="amount">
						<el-input v-model="form.amount" placeholder="预算金额" type="number" clearable>
							<template #append>
								RMB
							</template>
						</el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-form-item label="是否启用">
				<el-switch v-model="form.status" active-value="1" :nactive-value="0"></el-switch>
			</el-form-item>
			<el-form-item label="备注说明" prop="remark">
				<el-input v-model="form.remark" placeholder="备注说明" maxlength="380" clearable type="textarea" :rows="5"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">确 定</el-button>
		</template>
	</el-dialog>

</template>
<script>
export default {
	emits: ['success', 'closed'],
	components: {
	},
	data() {
		return {
			visible: false,
			isSaveing: false,
			isShowMsg: false,
			mode: "add",
			titleMap: {
				add: '年度费用预算设置',
				edit: '年度费用预算设置',
			},
			form: {
				recordId: '',
				title: '',
				expenseYear: '',
				amount: '',
				status: '1',
				remark: ''
			},
			rules: {
				title: [
					{required: true, message: '预算标题不能办空', trigger: 'blur'}
				],
				amount: [
					{required: true, message: '预算金额不能为空'}
				],
				expenseYear: [
					{required: true, message: '预算年度不能为空',trigger: 'blur'}
				]
			}
		}
	},
	mounted() {
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res;
					if(this.mode=="add") {
						res = await this.$API.finance.expense.insertExpenseComConfig.post(this.form);
					}else if(this.mode=="edit")
					{
						res = await this.$API.finance.expense.updateExpenseComConfig.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit('success')
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		setData(data) {
			this.form.recordId = data.recordId
			this.form.title = data.title
			this.form.expenseYear = data.expenseYear
			this.form.status = data.status
			this.form.remark = data.remark
			this.form.amount = data.amount
		},
	}
}
</script>

<style>
.customWidth {
	width: 60%;
}
</style>
